﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TimeSheetManagement.Models;

namespace TimeSheetManagement.Controllers
{ 
    public class ProjectController : Controller
    {
        private const int TAKE_PROJECTS = 10;
        private tsmdatabaseEntities db = new tsmdatabaseEntities();

        //
        // GET: /Project/

        public ViewResult Index()
        {
            var projects = db.Projects.Include(p => p.ProjectStatus);
            return View(projects.ToList());
        }

        //
        // GET: /Project/Details/5

        public ViewResult Details(Guid id)
        {
            Project project = db.Projects.Find(id);
            return View(project);
        }

        //
        // GET: /Project/Create

        public ActionResult Create()
        {
            ViewBag.ProjectStatusId = new SelectList(db.ProjectStatuses, "ProjectStatusId", "ProjectStatusName");
            return View();
        } 

        //
        // POST: /Project/Create

        [HttpPost]
        public ActionResult Create(Project project)
        {
            if (ModelState.IsValid)
            {
                project.ProjectId = Guid.NewGuid();
                db.Projects.Add(project);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.ProjectStatusId = new SelectList(db.ProjectStatuses, "ProjectStatusId", "ProjectStatusName", project.ProjectStatusId);
            return View(project);
        }
        
        //
        // GET: /Project/Edit/5
 
        public ActionResult Edit(Guid id)
        {
            Project project = db.Projects.Find(id);
            ViewBag.ProjectStatusId = new SelectList(db.ProjectStatuses, "ProjectStatusId", "ProjectStatusName", project.ProjectStatusId);
            return View(project);
        }

        //
        // POST: /Project/Edit/5

        [HttpPost]
        public ActionResult Edit(Project project)
        {
            if (ModelState.IsValid)
            {
                db.Entry(project).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.ProjectStatusId = new SelectList(db.ProjectStatuses, "ProjectStatusId", "ProjectStatusName", project.ProjectStatusId);
            return View(project);
        }

        //
        // GET: /Project/Delete/5
 
        public ActionResult Delete(Guid id)
        {
            Project project = db.Projects.Find(id);
            return View(project);
        }

        //
        // POST: /Project/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(Guid id)
        {            
            Project project = db.Projects.Find(id);
            db.Projects.Remove(project);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        public PartialViewResult Search(string q)
        {
            var projects = db.Projects
                            .Where(r => r.ProjectName.Contains(q)
                                    || String.IsNullOrEmpty(q)).Take(TAKE_PROJECTS);
            return PartialView("_ProjectSearchResults", projects);
        }

        public ActionResult QuickSearch(String term)
        {
            var projects = db.Projects
                                .Where(r => r.ProjectName.Contains(term))
                                .Take(TAKE_PROJECTS)
                                .Select(r => new { label = r.ProjectName });
            return Json(projects, JsonRequestBehavior.AllowGet);
        }

        public ActionResult JsonSearch(String q)
        {
            var projects = db.Projects
                                .Where(r => r.ProjectName.Contains(q)
                                     || String.IsNullOrEmpty(q)).Take(TAKE_PROJECTS)
                                .Select(r => new { 
                                                    r.ProjectName, r.Client, r.US_PM
                                                    });
            return Json(projects, JsonRequestBehavior.AllowGet);
        }
    }
}